/**
 * 
 */
package fpt.pits.dao;

import java.sql.SQLException;

import fpt.pits.dto.EmployeeInfo;
import fpt.pits.util.Connection;

/**
 * @author yoyosakura
 *
 */
public class EmployeeInfoDAO {
    public EmployeeInfo get(int empID) {
        EmployeeInfo employeeInfo = null;
        java.sql.Connection cn = null;
        java.sql.PreparedStatement ps = null;
        java.sql.ResultSet rs = null;
        String query = null;
        try {
            query = "SELECT e.* " +
            		"FROM `employee` e " +
            		"WHERE e.EmpID = ?";
            cn = Connection.getConnection();
            ps = cn.prepareStatement(query);
            ps.setInt(1, empID);
            rs = ps.executeQuery();
            while (rs.next()) {
                if (rs.getByte("IsDeleted") == 0) {
                    employeeInfo = new EmployeeInfo();
                    employeeInfo.setEmpID(rs.getInt("EmpID"));
                    employeeInfo.setName(rs.getString("Name"));
                    employeeInfo.setBirthday(rs.getString("BirthDay"));
                    employeeInfo.setAddress(rs.getString("Address"));
                    employeeInfo.setEmail(rs.getString("Email"));
                    employeeInfo.setPhone(rs.getString("Phone"));
                    employeeInfo.setGender(rs.getByte("Gender"));
                    employeeInfo.setDepartID(rs.getInt("DepartID"));
                    employeeInfo.setIsDeleted(rs.getByte("IsDeleted"));
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Get data from database " + e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (cn != null) {
                    cn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("Error: No close connection " + sqle.getMessage());
            }
        }
        return employeeInfo;
    }
}
